package com.homegoods.classify.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView.LayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.classify.R;
import com.homegoods.classify.item.ADViewItem;
import com.homegoods.classify.persenter.SubClassifyPresenter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.util.Utils;
import com.kedll.library.databinding.PullableListviewBinding;
import com.kedll.library.fragment.KRefreshListFragment;

/**
 * @author QiQi
 * @date 2017/5/2
 */
@Route(path = ARouterPath.PATH_CLASSIFY_CLASSIFY)
public class SubClassifyFragment extends KRefreshListFragment<PullableListviewBinding, SubClassifyPresenter> {

    @Autowired
    public String type;
    private SubClassifyPresenter recommendPresenter;

    @Override
    protected void init() {
        super.init();
        ARouter.getInstance().inject(this);
    }

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        super.initEvent(savedInstanceState);
        list.setBackgroundResource(R.color.white);
    }

    @Override
    protected LayoutManager getLayoutManager() {
        GridLayoutManager gridLayoutManager = Utils.getGridLayoutManager(getContext(), adapter, 3);
        GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {

                if (adapter.getItem(position) instanceof ADViewItem) {
                    return gridLayoutManager.getSpanCount();
                }

                if (spanSizeLookup != null) {
                    return spanSizeLookup.getSpanSize(position);
                }
                return 1;
            }
        });

        return gridLayoutManager;
    }

    public void setType(String type) {
        this.type = type;
        if (recommendPresenter != null) {
            recommendPresenter.setType(type);
        }
    }


    @Override
    public SubClassifyPresenter getPresenter() {
        if (recommendPresenter == null) {
            recommendPresenter = new SubClassifyPresenter(this, type);
        }
        return recommendPresenter;
    }

    @Override
    protected int getLayout() {
        return R.layout.pullable_listview;
    }

}
